// pages/friends/createAccount/index.ts
import tran from '../../../utils/language';
import { eqTestOverPut } from '../../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('recommendSkill'),
    region: {},
    isEdit: getApp().globalData.profileFrom === 'edit',
    filterData: [{ text: '自我意识', key: '321230', selectFlag: true }],
    questionTotalNum: 20,
    questionCurrentNum: 1,
    sliderNum: 1,
    partList: [
      {
        selected: false
      }
    ],
    partListAll: [
      {
        selected: false
      }
    ],
    info: {},
    headerText: '',
    isTest: true // 是否展示上方的重新情商测试的内容
  },
  // 返回
  goBack () {
    wx.navigateBack()
    // wx.navigateBack({
    //   delta: 1
    // })
  },
  /**
   * 计算滑块百分比
   * @param total 
   * @param num 
   */
  changeSilderNum (num: any, total: any) {
    let percentageNum: any = (num / total) * 100
    this.setData({
      sliderNum: percentageNum
    })
  },
  /**
   * 上一题
   */
  previousTap () {
    let { questionCurrentNum } = this.data
    questionCurrentNum--;
    // 调用计算滑块的值的方法
    this.changeSilderNum(questionCurrentNum, this.data.questionTotalNum)
    this.setData({
      questionCurrentNum: questionCurrentNum
    })
  },
  /**
   * 下一题
   */
  nextTap () {
    let { questionCurrentNum } = this.data
    questionCurrentNum++;
    // 调用计算滑块的值的方法
    this.changeSilderNum(questionCurrentNum, this.data.questionTotalNum)
    this.setData({
      questionCurrentNum: questionCurrentNum
    })
  },
  /**
   * 点击完成
   */
  testOver () {
    const { partList } = this.data
    const list:any = []
    partList.map((item: any) => {
      const obj = {
        eqSkillType: item.eqSkillType,
        selected: item.selected || false
      }
      list.push(obj)
    })
    const params = list
    eqTestOverPut(params).then(res => {
      wx.showToast({
        title: '提交成功'
      })
      const resStr = JSON.stringify(res)
      setTimeout(() => {
        wx.reLaunch({
          url: `/pages/profile/eqTestResult/index?info=${resStr}`
        })
      }, 2000)
    })
    // 
  },
  toSelectPage(e: any) {
    const currentIndex = e.target.dataset.index
    const { partList, partListAll } = this.data
    partList.length > 0 && partList.map((item, index) => {
      const selected = partListAll[index].selected
      if (!selected) { // 一开始默认选中的，就不能取消勾选，只有那些没有选中的，你可以选中或者取消选中
        if (index === currentIndex) {
          item.selected = !item.selected
        }
      }
    })
    this.setData({
      partList
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    if (options) {
      let info = JSON.parse(options.info)
      const list: any = []
      let num = 0;
      info.scoreAssessments.length > 0 && info.scoreAssessments.map((item: any) => {
        if (item.selected) {
          list.push(item.eqSkillTypeName)
          num++
        }
      })
      this.setData({
        info,
        headerText: list.length > 0 ? list.join('、') : '-',
        selectNum: num || 0,
        partList: JSON.parse(JSON.stringify(info.scoreAssessments)),
        partListAll: JSON.parse(JSON.stringify(info.scoreAssessments)),
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})